package com.lvhengchang.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lvhengchang.pojo.Card;
import com.lvhengchang.pojo.Result;
import com.lvhengchang.service.CardService;

@RestController
@RequestMapping("/card")
public class CardController {

	@Autowired
	private CardService cardService;

	/**
	 * 查询所有银行卡信息
	 */
	@GetMapping("/select")
	public Result<?> selectInfo() {
		List<Card> cards = cardService.selectCardList();
		if (cards != null) {
			return Result.success(cards, "selectall card success");
		} else {
			return Result.fail(500, "selectall card fail");
		}
	}

	@GetMapping("/selectid/{id}/{balance}")
	public Result<?> selectById(@PathVariable int id, @PathVariable("balance") String balance) {
		System.out.println("balance is" + balance);
		Card card = cardService.getCardById(id);
		if (card != null) {
			return Result.success(card, "select card success");
		} else {
			return Result.fail(500, "select card fail");
		}
	}

	/**
	 * 插入银行卡信息
	 * 
	 * @param user
	 * @return
	 */
	@PostMapping("/insert")
	public Result<?> insertInfo(@RequestBody Card card) throws Exception {
		int result = cardService.insertCard(card);
		if (result != 0) {
			return Result.success(result, "insert card success");
		} else {
			return Result.fail(500, "insert card fail");
		}
	}

	/**
	 * 删除银行卡信息
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping("/")
	public Result<?> deleteInfo(int id) {
		int result = cardService.deleteCard(id);
		if (result > 0) {
			return Result.success(result, "delete card success");
		} else {
			return Result.fail(500, "delete card fail");
		}
	}

	/**
	 * 根据所属人删除银行卡信息
	 * 
	 * @param belong
	 * @return
	 */
	@DeleteMapping("/deletebybelong")
	public Result<?> deleteByBelong(int belong) {
		int result = cardService.deleteCardByBelong(belong);
		if (result > 0) {
			return Result.success(result, "delete card by belong success");
		} else {
			return Result.fail(500, "delete card by belong fail");
		}
	}

	/**
	 * 修改银行卡信息
	 * 
	 * @param user
	 * @return
	 */
	@PutMapping("/update")
	public Result<?> updateInfo(@RequestBody Card card) {
		int result = cardService.updateById(card);
		if (result > 0) {
			return Result.success(result, "update card success");
		} else {
			return Result.fail(500, "update card fail");
		}
	}
}
